{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multi-phase chemical equilibria: sodium chloride precipitation\n",
    "In this notebook we will look at how ChemPy can formulate non-linear systems of equations based on precipitation. For more background on the non-linear equation system you may want to look at the notebook \"conditional\" in the [pyneqsys repo](https://github.com/bjodah/pyneqsys)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chempy.chemistry import Species, Equilibrium\n",
    "from chempy.equilibria import EqSystem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Na_p = Species('Na+', 1, composition={11: 1})\n",
    "Cl_m = Species('Cl-', -1, composition={17: 1})\n",
    "NaCl = Species('NaCl', phase_idx=1, composition={11: 1, 17: 1})\n",
    "eq = Equilibrium({'NaCl': 1}, {'Na+': 1, 'Cl-': 1}, 4.0)\n",
    "eqsys = EqSystem([eq], [Na_p, Cl_m, NaCl])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 5, 'Cl-': 5, 'NaCl': 0})\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 1, 'Cl-': 1, 'NaCl': 0})\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 2, 'Cl-': 2, 'NaCl': 0})\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 2})\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0})\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True)\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chempy.equilibria import NumSysLog\n",
    "sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True, NumSys=NumSysLog)\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chempy.equilibria import NumSysLin\n",
    "sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True, NumSys=(NumSysLog, NumSysLin))\n",
    "assert info['success'] and sane\n",
    "sol"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
